﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions;
using CrystalDecisions.Enterprise;
using CrystalDecisions.CrystalReports;
using CrystalDecisions.ReportSource;
using CrystalDecisions.CrystalReports.Engine;
using TI_BAL;
using CrystalDecisions.Shared;

namespace SecuritySystem.Reports.Datasets
{
    public partial class ReportFormWithFilter : Form
    {
        TIEntities dbContext;
        public string ReportType = "";

        public ReportFormWithFilter()
        {
            InitializeComponent();
            dbContext = new TIEntities();
        }

        private void ReportFormWithFilter_Load(object sender, EventArgs e)
        {
            if (ReportType == "JournalReport")
            {
                try
                {

                    //var Invoice = (from j in dbContext.SalesTransactions.Include("ProductType").Include("ProductSubType").Include("ProductSize") select new { TypeName = j.ProductType.Name, SubTypeName = j.ProductSubType.Name, SizeName = j.ProductSize.Name, j.PER_UNIT_PRICE, j.QUANITY, j.INVOICE_ID }).ToList();
                    
                    ///var listOfSuppliers = (from sup in dbContext.Suppliers where sup.ContactPersonName != "--" select new { sup.ContactPersonName, sup.CompanyName, sup.SupplierPhoneNumber, sup.SupplierEmailAddress, sup.City, sup.SupplierAddress }).ToList();

                    var listOfJournals = (from j in dbContext.Journals select new {j.Account.Account_Name , Journal_Amount = j.Journal_Amount.Value, Journal_Date = j.Journal_Date.Value, j.Journal_Description, TransactionType = j.TransactionType == "D" ? "Debit" : "Credit" }).ToList();



                    JournalReport reportobj = new JournalReport();

                    reportobj.SetDataSource(listOfJournals);

                    ParameterFieldDefinitions crParameterFieldDefinitions;
                    ParameterFieldDefinition crParameterFieldDefinition;
                    ParameterValues crParameterValues = new ParameterValues();
                    ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

                    crParameterDiscreteValue.Value = System.Configuration.ConfigurationSettings.AppSettings["CompanyName"];
                    crParameterFieldDefinitions = reportobj.DataDefinition.ParameterFields;
                    crParameterFieldDefinition = crParameterFieldDefinitions[0];
                    crParameterValues = crParameterFieldDefinition.CurrentValues;

                    crParameterValues.Clear();
                    crParameterValues.Add(crParameterDiscreteValue);
                    crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

                    ParameterFieldDefinitions crParameterFieldDefinitions1;
                    ParameterFieldDefinition crParameterFieldDefinition1;
                    ParameterValues crParameterValues1 = new ParameterValues();
                    ParameterDiscreteValue crParameterDiscreteValue1 = new ParameterDiscreteValue();

                    crParameterDiscreteValue1.Value = System.Configuration.ConfigurationSettings.AppSettings["CompanyAddress"];
                    crParameterFieldDefinitions1 = reportobj.DataDefinition.ParameterFields;
                    crParameterFieldDefinition1 = crParameterFieldDefinitions1[1];
                    crParameterValues1 = crParameterFieldDefinition1.CurrentValues;

                    crParameterValues1.Clear();
                    crParameterValues1.Add(crParameterDiscreteValue1);
                    crParameterFieldDefinition1.ApplyCurrentValues(crParameterValues1);

                    this.crystalReportViewer1.ReportSource = reportobj;
                }
                catch
                {

                }
            }
            
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (ReportType == "JournalReport")
            {
                try
                {
                    ///var listOfSuppliers = (from sup in dbContext.Suppliers where sup.ContactPersonName != "--" select new { sup.ContactPersonName, sup.CompanyName, sup.SupplierPhoneNumber, sup.SupplierEmailAddress, sup.City, sup.SupplierAddress }).ToList();

                    var listOfJournals = (from j in dbContext.Journals where j.Journal_Date >= this.dateTimePickerStart.Value && j.Journal_Date <= this.dateTimePickerLast.Value select new { j.Account.Account_Name, Journal_Amount = j.Journal_Amount.Value, Journal_Date = j.Journal_Date.Value, j.Journal_Description, TransactionType = j.TransactionType == "D" ? "Debit" : "Credit" }).ToList();
                                       

                    JournalReport reportobj = new JournalReport();

                    reportobj.SetDataSource(listOfJournals);

                    ParameterFieldDefinitions crParameterFieldDefinitions;
                    ParameterFieldDefinition crParameterFieldDefinition;
                    ParameterValues crParameterValues = new ParameterValues();
                    ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

                    crParameterDiscreteValue.Value = System.Configuration.ConfigurationSettings.AppSettings["CompanyName"];
                    crParameterFieldDefinitions = reportobj.DataDefinition.ParameterFields;
                    crParameterFieldDefinition = crParameterFieldDefinitions[0];
                    crParameterValues = crParameterFieldDefinition.CurrentValues;

                    crParameterValues.Clear();
                    crParameterValues.Add(crParameterDiscreteValue);
                    crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

                    ParameterFieldDefinitions crParameterFieldDefinitions1;
                    ParameterFieldDefinition crParameterFieldDefinition1;
                    ParameterValues crParameterValues1 = new ParameterValues();
                    ParameterDiscreteValue crParameterDiscreteValue1 = new ParameterDiscreteValue();

                    crParameterDiscreteValue1.Value = System.Configuration.ConfigurationSettings.AppSettings["CompanyAddress"];
                    crParameterFieldDefinitions1 = reportobj.DataDefinition.ParameterFields;
                    crParameterFieldDefinition1 = crParameterFieldDefinitions1[1];
                    crParameterValues1 = crParameterFieldDefinition1.CurrentValues;

                    crParameterValues1.Clear();
                    crParameterValues1.Add(crParameterDiscreteValue1);
                    crParameterFieldDefinition1.ApplyCurrentValues(crParameterValues1);

                    this.crystalReportViewer1.ReportSource = reportobj;
                }
                catch
                {

                }
            }
        }
    }
}
